package art.yulang.utils;

import art.yulang.annotation.Autowired;
import art.yulang.annotation.Component;

import java.sql.SQLException;

/**
 * 事务控制类：负责事务的开启、提交、回滚
 */
@Component("transactionManager")
public class TransactionManager {
    @Autowired
    private ConnectionUtil connectionUtil;

    /**
     * 手动事务事务开启
     * @throws SQLException
     */
    public void transactionBegin() throws SQLException {
        connectionUtil.getCurrentThreadConnection().setAutoCommit(false);
    }

    /**
     * 事务提交
     * @throws SQLException
     */
    public void commit() throws SQLException {
        connectionUtil.getCurrentThreadConnection().commit();
    }

    /**
     * 事务回滚
     * @throws SQLException
     */
    public void rollback() throws SQLException {
        connectionUtil.getCurrentThreadConnection().rollback();
    }
}
